package com.ruoyi.system.service.equipment.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.equipment.EquipmentBasic;
import com.ruoyi.system.mapper.equipment.EquipmentBasicMapper;
import com.ruoyi.system.service.equipment.IEquipmentBasicService;
import org.bouncycastle.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * 主要设备Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-17
 */
@Service
public class EquipmentBasicServiceImpl implements IEquipmentBasicService
{
    @Autowired
    private EquipmentBasicMapper equipmentBasicMapper;

    /**
     * 查询主要设备
     * 
     * @param id 主要设备主键
     * @return 主要设备
     */
    @Override
    public EquipmentBasic selectEquipmentBasicById(Long id)
    {
        return equipmentBasicMapper.selectEquipmentBasicById(id);
    }

    /**
     * 查询主要设备列表
     * 
     * @param equipmentBasic 主要设备
     * @return 主要设备
     */
    @Override
    public List<EquipmentBasic> selectEquipmentBasicList(EquipmentBasic equipmentBasic)
    {
        if (StringUtils.isNotEmpty(equipmentBasic.getIds())) {
            equipmentBasic.setIdAry( Arrays.stream(equipmentBasic.getIds().split(","))
                    .map(Long::parseLong).toArray(Long[]::new));
        }
        return equipmentBasicMapper.selectEquipmentBasicList(equipmentBasic);
    }

    /**
     * 新增主要设备
     * 
     * @param equipmentBasic 主要设备
     * @return 结果
     */
    @Override
    public int insertEquipmentBasic(EquipmentBasic equipmentBasic)
    {
        equipmentBasic.setCreatedDate(new Date());
        equipmentBasic.setCreatedBy(SecurityUtils.getUsername());
        return equipmentBasicMapper.insertEquipmentBasic(equipmentBasic);
    }

    /**
     * 修改主要设备
     * 
     * @param equipmentBasic 主要设备
     * @return 结果
     */
    @Override
    public int updateEquipmentBasic(EquipmentBasic equipmentBasic)
    {
        equipmentBasic.setModifyDate(new Date());
        equipmentBasic.setModifyBy(SecurityUtils.getUsername());
        return equipmentBasicMapper.updateEquipmentBasic(equipmentBasic);
    }

    /**
     * 批量删除主要设备
     * 
     * @param ids 需要删除的主要设备主键
     * @return 结果
     */
    @Override
    public int deleteEquipmentBasicByIds(Long[] ids)
    {
        return equipmentBasicMapper.deleteEquipmentBasicByIds(ids);
    }

    /**
     * 删除主要设备信息
     * 
     * @param id 主要设备主键
     * @return 结果
     */
    @Override
    public int deleteEquipmentBasicById(Long id)
    {
        return equipmentBasicMapper.deleteEquipmentBasicById(id);
    }



    /**
     * 分配检查人员
     *
     * @param list 检查人员
     * @return 查询结果
     */
    @Override
    public String importHandler(List<EquipmentBasic> list, String loginUser) {
        if (StringUtils.isEmpty(list)) {
            throw new ServiceException("导入设备数据不能为空！");
        }
        list.forEach(e -> {
            e.setCreatedBy(loginUser);
            e.setCreatedDate(new Date());
        });
        equipmentBasicMapper.insertBatch(list);
        return "成功";
    }
}
